﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Common.Background.Models
{
    /// <summary>
    /// 面包屑导航
    /// 树型结构
    /// </summary>
    public class BreadcrumbNavigation
    {
        public BreadcrumbNavigation()
        { }

        public BreadcrumbNavigation(int id, string name, string linkUrl, bool isCurrent, int? parentID)
        {
            this.ID = id;
            this.Name = name;
            this.LinkUrl = linkUrl;
            this.IsCurrent = isCurrent;
            this.ParentID = parentID;
        }
        public int ID { get; set; }
        public string Name { get; set; }
        public string LinkUrl { get; set; }
        public bool IsCurrent { get; set; }
        public int? ParentID { get; set; }
        public List<BreadcrumbNavigation> Sons { get; set; }
        public BreadcrumbNavigation Father { get; set; }

        public static List<BreadcrumbNavigation> Data = new List<BreadcrumbNavigation>();
        /// <summary>
        /// 初始化，填充基础数据
        /// </summary>
        public static void Init()
        {
            Data.Add(new BreadcrumbNavigation(1, "网站首页", "#", false, null));
            Data.Add(new BreadcrumbNavigation(2, "我的学习教程", "/StudyCenter/Index/0-1.shtml", false, 6));
            Data.Add(new BreadcrumbNavigation(3, "已购买的课程", "/StudyCenter/Index/0-1.shtml", false, 2));
            Data.Add(new BreadcrumbNavigation(4, "已到期的课程", "/StudyCenter/Index/1-1.shtml", false, 2));
            Data.Add(new BreadcrumbNavigation(5, "已观看的课程", "/StudyCenter/Index/2-1.shtml", false, 2));
            Data.Add(new BreadcrumbNavigation(6, "用户中心", "#", false, 1));
            Data.Add(new BreadcrumbNavigation(7, "在线一对一学习", "#", false, 6));
            Data.Add(new BreadcrumbNavigation(8, "发布需求", "/OneToOne/StuPublishRequirement.shtml", false, 7));
            Data.Add(new BreadcrumbNavigation(9, "我的预约", "#", false, 7));
            Data.Add(new BreadcrumbNavigation(10, "我的辅导需求", "/OneToOne/StuRequirementList.shtml", false, 9));
            Data.Add(new BreadcrumbNavigation(11, "全部应约老师", "/OneToOne/StuRequirement?iReqID={0}", false, 9));
            Data.Add(new BreadcrumbNavigation(12, "继续学习", "/StudyCenter/Index/0-1.shtml", false, 2));
            Data.Add(new BreadcrumbNavigation(13, "我的收藏", "/Collection/CollectionIndex.shtml", false, 6));
            Data.Add(new BreadcrumbNavigation(14, "学前检测", "#", false, 6));
            Data.Add(new BreadcrumbNavigation(15, "我的直播课程", "/StudyCenter/Index/2-1.shtml", false, 2));
            Data.Add(new BreadcrumbNavigation(16, "我的体验", "#", false, 6));
            Data.Add(new BreadcrumbNavigation(17, "我的账户", "/FinanceCenter/StudentIndex", false, 6));
            Data.Add(new BreadcrumbNavigation(18, "交易记录", "/FinanceCenter/StuConsumption", false, 17));
            Data.Add(new BreadcrumbNavigation(19, "充值记录", "/FinanceCenter/StuRechargeList", false, 17));
            Data.Add(new BreadcrumbNavigation(20, "绑定听课证", "/FinanceCenter/BindingCard", false, 17));
            Data.Add(new BreadcrumbNavigation(21, "充值", "/FinanceCenter/StudentRecharge", false, 17));
            Data.Add(new BreadcrumbNavigation(22, "充值成功", "/FinanceCenter/StudentRechargeSuccess", false, 17));
            Data.Add(new BreadcrumbNavigation(23, "充值失败", "/FinanceCenter/StudentRechargeFail", false, 17));
            Data.Add(new BreadcrumbNavigation(24, "我的问答", "#", false, 6));
            Data.Add(new BreadcrumbNavigation(25, "我的订单", "/UserOrder/Order", false, 6));
            Data.Add(new BreadcrumbNavigation(26, "个人资料设置", "/UserProfile/UserBasicInfoManage", false, 6));
            Data.Add(new BreadcrumbNavigation(27, "账户安全管理", "/UserProfile/UserPassWordManage", false, 6));
            Data.Add(new BreadcrumbNavigation(28, "我联系的老师", "#", false, 7));
            Data.Add(new BreadcrumbNavigation(29, "联系我的老师", "#", false, 7));


        }
    }

    /// <summary>
    /// 树的操作
    /// </summary>
    public class Tree
    {
        List<BreadcrumbNavigation> Data;
        public Tree(List<BreadcrumbNavigation> data)
        {
            this.Data = data;
        }

        #region 树的装载
        public BreadcrumbNavigation GetCompleteTree()
        {
            var root = Data.First(i => !i.ParentID.HasValue);
            GetSubTree(root);
            return root;
        }

        void GetSubTree(BreadcrumbNavigation tree)
        {
            List<BreadcrumbNavigation> sons = Data.Where(i => i.ParentID == tree.ID).ToList();
            tree.Sons = sons;
            sons.ForEach(i =>
            {
                i.Father = tree;
                GetSubTree(i);
            });
        }
        #endregion

        #region 得到不完整的树
        public BreadcrumbNavigation GetTree(int id)
        {
            var root = Data.First(i => i.ID == id);
            GetSubTree(root);
            return root;
        }
        #endregion

        #region 通过叶子节点得到它所有祖辈节点
        public BreadcrumbNavigation GetAllFather(int id)
        {
            var root = Data.FirstOrDefault(i => i.ID == id);
            if (root != null)
            {
                GetFatherTree(root);
            }
            return root;
        }

        void GetFatherTree(BreadcrumbNavigation tree)
        {
            BreadcrumbNavigation father = Data.FirstOrDefault(i => i.ID == tree.ParentID);
            if (father != null)
            {
                tree.Father = father;
                GetFatherTree(father);
            }
        }
        #endregion
    }
}